package com.sfzy.controller;

import com.github.pagehelper.PageInfo;
import com.sfzy.entity.Chapters;
import com.sfzy.service.CertificateService;
import com.sfzy.service.ChaptersService;
import com.sfzy.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("chapters")
public class ChaptersController {
    @Autowired
    private ChaptersService chaptersService;
    //增加
    @PostMapping("add")
    public R insertChapters(@RequestBody Chapters chapters) {
        int num = chaptersService.insertChapters(chapters);
        return R.ok(num);
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteChapters(@PathVariable("id") int id) {
        int num = chaptersService.deleteChapters(id);
        return R.ok(num);
    }
    //修改
    @PutMapping("update")
    public R updateChapters(@RequestBody Chapters chapters) {
        int num = chaptersService.updateChapters(chapters);
        return R.ok(num);
    }
    //根据id查询
    @GetMapping("{id}")
    public R getCertificate(@PathVariable("id") int id) {
        List<Chapters> chapters = chaptersService.selectChaptersById3(id);
        return R.ok(chapters);
    }
    //分页查询
    @GetMapping("page3")
    public R getCertificates(Chapters chapters,
                             @RequestParam(required = false,defaultValue = "1")int pageNum,
                             @RequestParam(required = false,defaultValue = "3")int pageSize) {
        PageInfo<Chapters> pageInfo = chaptersService.selectChapters3(chapters, pageNum, pageSize);
        return R.ok(pageInfo);
    }
}
